home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / What's New? / Sample Code / Graphics 2D / CopyDeepMask / CopyDeepMask.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  6.8 KB  |  267 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        CopyDeepMask.c
  3.  
  4.     Contains:    This snippet demonstrates the use of CopyDeepMask using 2 PICTS;
  5.                  one a photograph and the other a triangular mask.     It uses 2 offscreen
  6.                  gworlds to hold the source and mask pixmaps.  CopyDeepMask is then used
  7.                  to create the masked image and display it in the application window.  The
  8.                  source, mask, and destination rectangles are all the same size in order
  9.                  avoid altering pixel sizes.
  10.  
  11.     Written by: KM    
  12.  
  13.     Copyright:    Copyright © 1993-1999 by Apple Computer, Inc., All Rights Reserved.
  14.  
  15.                 You may incorporate this Apple sample source code into your program(s) without
  16.                 restriction. This Apple sample source code has been provided "AS IS" and the
  17.                 responsibility for its operation is yours. You are not permitted to redistribute
  18.                 this Apple sample source code as "Apple sample source code" after having made
  19.                 changes. If you're going to re-distribute the source, we require that you make
  20.                 it clear in the source that the code was descended from Apple sample source
  21.                 code, but that you've made changes.
  22.  
  23.     Change History (most recent first):
  24.                 08/2000        JM                Carbonized, non-Carbon code is commented out
  25.                                             for demonstration purposes.
  26.                 7/9/1999    KG                Updated for Metrowerks Codewarror Pro 2.1
  27.                 12/14/94     KM                Fixed a bug in doEventLoop that was causing a bus error using 
  28.                                              EvenBetterBusError (bad things happen when you attempt to use an 
  29.                                              unitialized EventRecord!)
  30.                 11/22/93    KM                Created        
  31.                 
  32.  
  33. */
  34. #include "CarbonPrefix.h"
  35. #include <Quickdraw.h>
  36. #include <QDOffscreen.h>
  37. #include <Windows.h>
  38. #include <Dialogs.h>
  39. #include <Menus.h>
  40. #include <Types.h>
  41. #include <Memory.h>
  42. #include <Fonts.h>
  43. //#include <OSEvents.h>
  44. #include <Events.h>
  45. #include <ToolUtils.h>
  46.  
  47. #define windID 128
  48. #define inFront -1
  49. #define sleepTime 30
  50.  
  51. #define     srcPicID    130
  52. #define     maskPicID    131
  53.  
  54. Boolean                    continueThis;
  55. WindowPtr                mainWindow;
  56.  
  57. void InitToolbox(void);
  58. void doEventLoop(void);
  59. void doDrag (WindowPtr winPtr,Point mouseLoc);
  60. void doUpdateEvent(EventRecord *event);
  61.  
  62.  
  63. void main(void)
  64. {
  65.     
  66.     InitToolbox();
  67.         
  68.     mainWindow = GetNewCWindow(windID,nil,(WindowPtr)inFront);
  69.     
  70.     continueThis = true;
  71.     
  72.     doEventLoop();
  73. }
  74.  
  75. void InitToolbox(void)
  76. {
  77.     /*MoreMasters();
  78.     MoreMasters();
  79.     MoreMasters();
  80.     
  81.     MaxApplZone();
  82.  
  83.     InitGraf((Ptr)&qd.thePort);
  84.     InitFonts();
  85.     InitWindows();
  86.     InitMenus();
  87.     //InitDialogs((long)nil);
  88.     TEInit();*/
  89.     
  90.     MoreMasterPointers(3);
  91.     
  92.     InitCursor();
  93.  
  94.     FlushEvents(everyEvent, 0);
  95.     
  96. }
  97.  
  98. void doEventLoop(void)
  99. {
  100.     EventRecord    event;
  101.     WindowPtr    window;
  102.     short        hitArea;
  103.         
  104.     
  105.     while (continueThis)
  106.     {    
  107.         if (WaitNextEvent(everyEvent,&event,sleepTime,nil))
  108.             
  109.             if (event.what == updateEvt)    
  110.                 doUpdateEvent(&event);
  111.                 
  112.             else if (event.what == mouseDown)
  113.             {
  114.                 hitArea = FindWindow(event.where,&window);
  115.                 
  116.                 if (hitArea == inDrag)
  117.                     doDrag(window,event.where);
  118.                     
  119.                 else if (hitArea == inGoAway)
  120.                     if (TrackGoAway (window,event.where))
  121.                         return;
  122.             }
  123.             
  124.     } 
  125. }
  126.         
  127. void doDrag (WindowPtr winPtr,Point mouseLoc)
  128. {
  129.     Rect     dragRect;
  130.     Rect    bndsRect;
  131.     BitMap    bitMap;
  132.     
  133.     //bndsRect = qd.screenBits.bounds; /*screenBits is a QuickDraw global variable with the same structure as portBits (BitMap)*/
  134.     bndsRect = GetQDGlobalsScreenBits(&bitMap)->bounds;    //must use accessor functions to get at the global Quickdraw variable in Carbon
  135.     InsetRect(&dragRect,10,10);
  136.     DragWindow(winPtr,mouseLoc,&bndsRect);
  137.     
  138. }
  139.  
  140. void doUpdateEvent(EventRecord *event)
  141. {
  142. #pragma unused (event)
  143.  
  144.     Rect            bndsRectSrc,bndsRectMask,srcRect,maskRect,destRect;
  145.     QDErr            gWorldErr;
  146.     GWorldPtr        offscreenGWorldSource;
  147.     GWorldPtr        offscreenGWorldMask;
  148.     short            pixelDepthSource = 32;
  149.     short            pixelDepthMask = 1;
  150.     GWorldFlags        flags = 0;
  151.     GDHandle        currGDevice;
  152.     GWorldPtr        currGWorldPort;
  153.     PicHandle        srcPicHdl;
  154.     PicHandle        maskPicHdl;
  155.     PixMapHandle    srcPMHdl;
  156.     PixMapHandle    maskPMHdl;
  157.     Boolean            pmLock= false;
  158.     GrafPtr            oldPort;
  159.     
  160.     GetPort(&oldPort);
  161.  
  162.     
  163.     //Set up the bounds rectangle for the source and mask gWorlds    
  164.     bndsRectSrc.top = 32;
  165.     bndsRectSrc.left = 64;
  166.     bndsRectSrc.bottom = 160;
  167.     bndsRectSrc.right = 192;
  168.     
  169.     bndsRectMask = bndsRectSrc;
  170.     
  171.     //Make the source,mask and destination rectangles the same 
  172.     // size as the associated gWorlds
  173.     srcRect = bndsRectSrc;
  174.     maskRect = bndsRectSrc;
  175.     destRect = bndsRectSrc;
  176.     
  177.     //Fetch the current port and gdevice and save them for later
  178.     GetGWorld(&currGWorldPort,&currGDevice);
  179.         
  180.     //Set up our source and mask gWorlds
  181.     gWorldErr = NewGWorld(&offscreenGWorldSource,pixelDepthSource,&bndsRectSrc,0,nil,flags);
  182.     if(gWorldErr != noErr)
  183.         DebugStr("\pthe first NewGWorld call failed");
  184.     else 
  185.     {
  186.         //lock the offscreen buffer
  187.         srcPMHdl = GetGWorldPixMap(offscreenGWorldSource);
  188.         pmLock = LockPixels(srcPMHdl);
  189.         if (!pmLock)
  190.             DebugStr("\pthe first LockPixels call failed");
  191.     }
  192.         
  193.     gWorldErr = NewGWorld(&offscreenGWorldMask,pixelDepthMask,&bndsRectMask,0,nil,flags);
  194.     if(gWorldErr != noErr)
  195.         DebugStr("\pthe second NewGWorld call failed"); 
  196.     else 
  197.     {
  198.         //lock the offscreen buffer
  199.         maskPMHdl = GetGWorldPixMap(offscreenGWorldMask);
  200.         pmLock = LockPixels(maskPMHdl);
  201.         if (!pmLock)
  202.             DebugStr("\pthe second LockPixels call failed");
  203.     }
  204.         
  205.     //Set the current graphics world to my offscreen source and draw into it
  206.     SetGWorld((CGrafPtr) offscreenGWorldSource,nil);
  207.     
  208.     srcPicHdl = GetPicture(srcPicID);
  209.     if (srcPicHdl==nil)
  210.         DebugStr("\pthe first call to GetPicture failed");
  211.     EraseRect(&srcRect);
  212.     HLock((Handle)srcPicHdl);
  213.     DrawPicture(srcPicHdl,&srcRect);
  214.     HUnlock((Handle) srcPicHdl);
  215.     
  216.     //Set the current graphics world to my offscreen mask and draw into it
  217.     SetGWorld((CGrafPtr)offscreenGWorldMask,nil);
  218.     GetPicture(maskPicID);
  219.     maskPicHdl = GetPicture(maskPicID);
  220.     if (maskPicHdl==nil)
  221.         DebugStr("\pthe second call to GetPicture failed");
  222.     EraseRect(&maskRect);
  223.     HLock((Handle)maskPicHdl);
  224.     DrawPicture(maskPicHdl,&maskRect);
  225.     HUnlock((Handle)maskPicHdl);
  226.  
  227.     //Set the current graphics port to my application window for drawing into
  228.     SetGWorld((CGrafPtr)mainWindow,nil);
  229.     SetPort(GetWindowPort(mainWindow));
  230.     BeginUpdate(mainWindow);
  231.  
  232.     //Now for the whole point of this. . .call CopyDeepMask
  233.     /*CopyDeepMask(
  234.                 (BitMap *) &(offscreenGWorldSource->portPixMap),
  235.                 (BitMap *) &(offscreenGWorldMask->portPixMap),
  236.                 &(mainWindow->portBits),
  237.                 &srcRect,
  238.                 &maskRect,
  239.                 &destRect,
  240.                 srcCopy,
  241.                 nil);*/
  242.                 
  243.     CopyDeepMask(
  244.                 (BitMap *) *(GetPortPixMap(offscreenGWorldSource)),
  245.                 (BitMap *) *(GetPortPixMap(offscreenGWorldMask)),
  246.                 GetPortBitMapForCopyBits(GetWindowPort(mainWindow)),
  247.                 &srcRect,
  248.                 &maskRect,
  249.                 &destRect,
  250.                 srcCopy,
  251.                 nil);
  252.                 
  253.     EndUpdate(mainWindow);
  254.     
  255.     //Unlock the offscreen buffer
  256.     UnlockPixels(srcPMHdl);
  257.     UnlockPixels(maskPMHdl);
  258.     
  259.     //Restore the saved port and gdevice
  260.     SetGWorld(currGWorldPort,currGDevice);
  261.         
  262.     //Dispose of the gWorlds
  263.     DisposeGWorld(offscreenGWorldSource);
  264.     DisposeGWorld(offscreenGWorldMask);
  265.     SetPort(oldPort);
  266.  
  267. }